package com.sheng.center.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.sheng.center.util.Pager;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sheng.center.entity.Lab;
import com.sheng.center.service.LabService;

public class LabAction extends ActionSupport implements ModelDriven<Lab>{

	private static final long serialVersionUID = -631546119581451465L;
	protected String forwardView;
	protected static final String ADD_JSP = "/WEB-INF/page/Lab_add.jsp";
	protected static final String EDIT_JSP = "/WEB-INF/page/Lab_edit.jsp";
	protected static final String LIST_JSP = "/WEB-INF/page/Lab_list.jsp";
    private Lab lab = new Lab();
    private List<Lab> labList;
    private Pager<Lab> page;
    private String number;
	private Integer pageNo;
	private LabService labService;
	@Override
	public Lab getModel() {
		return lab;
	}
	
	
	/**
	 * 
	 * @Title: openAdd、openEdit
	 * @Description: 拦截动作打开对应页面
	 * @param @return 参数
	 * @return String 返回类型
	 * @author huangshengjun 1532950421@qq.com
	 * @date 2019年12月10日 下午8:36:29 
	 * @throws
	 */
	public String openAdd(){
		setForwardView(ADD_JSP);
		return SUCCESS;
	}
	public String openEdit() {
		System.out.println("===需要修改的实验室id==="+lab.getId());
		lab = labService.findById(lab);
		setForwardView(EDIT_JSP);
		return SUCCESS;
	}
	
	/**
	 * 
	 * @Title: 
	 * @Description: list列表，add添加，edit修改，delete删除
	 * @param @return 参数
	 * @return String 返回类型
	 * @author huangshengjun 1532950421@qq.com
	 * @date 2019年12月10日 下午8:37:20 
	 * @throws
	 */
//	public String list() {
//		labList = labService.findAllList(lab);
//		setForwardView(LIST_JSP);
//		return SUCCESS;
//	}
	
	/**
	 * 数据库为空的时候，这个查询功能汇报500
	 * @return
	 */
	public String list() {
		HttpServletRequest request = ServletActionContext.getRequest();
		labList = labService.findAllList(lab);
		number = request.getParameter("pageNumber");//计算前后页码
		if(number != null&&!number.equals("")) {
			pageNo = Integer.parseInt(number);
		}
		pageNo=(pageNo==null?1:pageNo);
		page = new Pager<Lab>(5,pageNo,labList);//每页多少数据，
		System.out.println("分页后的lsit"+page.getList().size());
		request.setAttribute("result", page);
		
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	
	public String add() {
		labService.add(lab);
		return list();
	}
	
	public String edit() {
		labService.edit(lab);
		return list();
	}
	/**
	 * 
	 * @Title: delete
	 * @Description: 根据id查询这条信息并赋值，然后在daoImpl进行删除操作
	 * @param @return 参数
	 * @return String 返回类型
	 * @author huangshengjun 1532950421@qq.com
	 * @date 2019年12月11日 下午5:14:45 
	 * @throws
	 */
	public String delete() {
		lab = labService.findById(lab);
		System.out.println("===需要删除的实验室id==="+lab.getId()+";"+lab.getLabId()+";"+lab.getLabName());
		labService.delete(lab);
		setForwardView(LIST_JSP);
		return SUCCESS;
	}
	

	public String getForwardView() {
		return forwardView;
	}

	public void setForwardView(String forwardView) {
		this.forwardView = forwardView;
	}

	public Lab getLab() {
		return lab;
	}

	public void setLab(Lab lab) {
		this.lab = lab;
	}

	public LabService getLabService() {
		return labService;
	}

	public void setLabService(LabService labService) {
		this.labService = labService;
	}
	public List<Lab> getLabList() {
		return labList;
	}
	public void setLabList(List<Lab> labList) {
		this.labList = labList;
	}


	public String getNumber() {
		return number;
	}


	public void setNumber(String number) {
		this.number = number;
	}


	public Integer getPageNo() {
		return pageNo;
	}


	public Pager<Lab> getPage() {
		return page;
	}


	public void setPage(Pager<Lab> page) {
		this.page = page;
	}


	public void setPageNo(Integer pageNo) {
		this.pageNo = pageNo;
	}
}
